import 'package:flutter/material.dart';
import 'package:dynamic_tabbar/dynamic_tabbar.dart';
import 'package:get/get_connect/http/src/utils/utils.dart';
import 'package:my_first_flutter_app/list_todo_cards.dart';

enum SampleMenuItem { itemOne, itemTwo, itemThree }

class DynamicTabBarDemo extends StatefulWidget {
  const DynamicTabBarDemo({super.key, required this.title});
  final String title;
  @override
  State<StatefulWidget> createState() => _DynamicTabBarDemoState();
}

class _DynamicTabBarDemoState extends State<DynamicTabBarDemo> {
  bool isScrollable = true;
  bool showNextIcon = false;
  bool showBackIcon = false;

  var tabs = <TabData>[
    TabData(
        index: 1,
        title: const Tab(
          child: Text(
            '需求评审中阿水淀粉撒地方',
            softWrap: false,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        content: ListTodoCards()),
    TabData(
      index: 2,
      title: const Tab(
        child: Text(
          '开发中',
          softWrap: false,
        ),
      ),
      content: ListTodoCards(),
    ),
    TabData(
      index: 3,
      title: const Tab(
        child: Text(
          '测试中大的点点滴滴',
          softWrap: false,
        ),
      ),
      content: const Center(child: Text('Content for Tab 2')),
    ),
  ];
  void accountInfo() {}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        centerTitle: false,
        actions: <Widget>[
          IconButton(
              onPressed: accountInfo,
              isSelected: false,
              icon: const Icon(Icons.account_circle)),
        ],
      ),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: const <Widget>[
            DrawerHeader(
              decoration: BoxDecoration(color: Colors.white10),
              child: Text(
                "项目列表",
                style: TextStyle(color: Colors.white),
              ),
            ),
            ListTile(
              title: Text("app开发"),
            ),
          ],
        ),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Expanded(
            child: DynamicTabBarWidget(
              dynamicTabs: tabs,
              isScrollable: isScrollable,
              onTabControllerUpdated: (controller) {
                debugPrint("onTabControllerUpdated");
              },
              onTabChanged: (index) {
                Scrollable.ensureVisible(context,
                    alignment: 0.5,
                    duration: const Duration(milliseconds: 300));
                debugPrint("onTabChanged $index");
              },
              indicatorSize: TabBarIndicatorSize.label,
              onAddTabMoveTo: MoveToTab.last,
              showBackIcon: showBackIcon,
              showNextIcon: showNextIcon,
              trailing: MenuAnchor(
                  builder: (BuildContext context, MenuController controller,
                      Widget? child) {
                    return IconButton(
                      onPressed: () {
                        if (controller.isOpen) {
                          controller.close();
                        } else {
                          controller.open();
                        }
                      },
                      icon: const Icon(Icons.more_vert),
                    );
                  },
                  menuChildren: <MenuItemButton>[
                    MenuItemButton(
                        child: const Text("新增列表"),
                        onPressed: () => {debugPrint("新增列表")}),
                        
                    MenuItemButton(
                        child: const Text("删除列表"),
                        onPressed: () => {debugPrint("新增列表")}),
                  ]),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => {},
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  void addTab() {
    setState(() {
      var tabNumber = tabs.length + 1;
      tabs.add(
        TabData(
          index: tabNumber,
          title: Tab(
            child: Text('Tab $tabNumber'),
          ),
          content: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Dynamic Tab $tabNumber'),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () => removeTab(tabNumber - 1),
                child: const Text('Remove this Tab'),
              ),
            ],
          ),
        ),
      );
    });
  }

  void scrollToCenter(int index) {}
  void removeTab(int index) {
    setState(() {
      tabs.removeAt(index);
    });
  }
}
